'''
@Author: your name
@Date: 2020-06-10 15:20:29
@LastEditTime: 2020-06-17 09:44:22
@LastEditors: Please set LastEditors
@Description: In User Settings Edit
@FilePath: \common_web\web\app\app.py
'''
import redis
from flask import Flask
from redis import ConnectionPool

# 导入蓝图集合
from .blueprints import blueprints
from .config_default import DefaultConfig
from .extensions import db, session_redis,login_manager

# For import *
__all__ = ['create_app']

def create_app(app_name=None):
    app = Flask(__name__)
    app.config.from_object(DefaultConfig)
    
    # 初始化数据库
    db.init_app(app)

    # 初始化 redis
    redis.connection_pool = ConnectionPool(**app.config.get('REDIS_CONFIG'))
    session_redis.connection_pool = ConnectionPool(
        **app.config.get('SESSION_REDIS'))

    # 初始化登陆管理
    login_manager.init_app(app)
    login_manager.login_view = '/admin/author/login'
    login_manager.login_message = '你必须登陆后才能访问该页面'
    login_manager.login_message_category = "info"

    # 注册蓝图
    configure_blueprints(app, app_name, blueprints)

    return app

def configure_blueprints(app, app_name, blueprints):
    for bp in blueprints:
        app.register_blueprint(bp)
